/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.servlet;

import com.util.SQLConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author chetans
 */
public class TaskListServlet extends HttpServlet {
   
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private Logger myLogger = Logger.getLogger("RootLogger");

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        myLogger.info("Enetering..");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            getTaskList(out);
        }catch(Exception e){
            e.printStackTrace();
            myLogger.error("Exception e"+e);
        }
        myLogger.info("Exiting..");
    } 

    private Connection con;
    private PreparedStatement pstmt;
    private ResultSet rs;

    private void getTaskList(PrintWriter out)throws Exception {
       myLogger.info("Entering..");
       con = new SQLConnection().getMySQLConnection();
       pstmt = con.prepareStatement("select * from tasks ");
       rs = pstmt.executeQuery();
       String outPut = "";
       while(rs.next()){
           outPut =outPut+rs.getString("task_name")+":";
       }
       myLogger.info("Tasks are : "+outPut);
       out.println(outPut);
       myLogger.info("Exiting..");
    }
}
